package com.example.cursor_controller;

import com.example.cursor_service.RoleService;
import com.example.cursor_orm.model.Role;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/roles")
@Tag(name = "角色管理", description = "角色相关的API接口")
public class RoleController {
    private final RoleService roleService;
    
    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }
    
    @PostMapping
    @Operation(summary = "创建角色", description = "创建新角色")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "角色创建成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public Role createRole(@RequestBody Role role) { 
        return roleService.createRole(role); 
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取角色", description = "根据角色ID获取角色信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取角色成功"),
        @ApiResponse(responseCode = "404", description = "角色不存在")
    })
    public Role getRoleById(@Parameter(description = "角色ID") @PathVariable Integer id) { 
        return roleService.getRoleById(id); 
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新角色", description = "根据角色ID更新角色信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "角色更新成功"),
        @ApiResponse(responseCode = "404", description = "角色不存在")
    })
    public Role updateRole(@Parameter(description = "角色ID") @PathVariable Integer id, @RequestBody Role role) { 
        return roleService.updateRole(id, role); 
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色", description = "根据角色ID删除角色")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "角色删除成功"),
        @ApiResponse(responseCode = "404", description = "角色不存在")
    })
    public boolean deleteRole(@Parameter(description = "角色ID") @PathVariable Integer id) { 
        return roleService.deleteRole(id); 
    }
    
    @GetMapping
    @Operation(summary = "获取角色列表", description = "分页获取角色列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取角色列表成功")
    })
    public List<Role> getRoleList(@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page, 
                                 @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") int size) { 
        return roleService.getRoleList(page, size); 
    }
    
    @GetMapping("/{id}/users")
    @Operation(summary = "获取角色的用户列表", description = "根据角色ID获取该角色下的用户ID列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取用户列表成功"),
        @ApiResponse(responseCode = "404", description = "角色不存在")
    })
    public List<Integer> getUserIdsByRoleId(@Parameter(description = "角色ID") @PathVariable Integer id, 
                                          @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page, 
                                          @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") int size) { 
        return roleService.getUserIdsByRoleId(id, page, size); 
    }
    
    @PostMapping("/{id}/users")
    @Operation(summary = "为用户分配角色", description = "为用户分配指定角色")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "角色分配成功"),
        @ApiResponse(responseCode = "404", description = "角色或用户不存在")
    })
    public boolean assignRoleToUser(@Parameter(description = "角色ID") @PathVariable Integer id, 
                                   @Parameter(description = "用户ID") @RequestParam Integer userId) { 
        return roleService.assignRoleToUser(id, userId); 
    }
    
    @DeleteMapping("/{id}/users/{userId}")
    @Operation(summary = "移除用户角色", description = "移除用户的指定角色")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "角色移除成功"),
        @ApiResponse(responseCode = "404", description = "角色或用户不存在")
    })
    public boolean removeRoleFromUser(@Parameter(description = "角色ID") @PathVariable Integer id, 
                                     @Parameter(description = "用户ID") @PathVariable Integer userId) { 
        return roleService.removeRoleFromUser(id, userId); 
    }
} 